package nc.impl.dm.m4804.rule;

import java.util.Map;
import nc.bs.ml.NCLangResOnserver;
import nc.impl.pubapp.pattern.rule.IRule;
import nc.vo.dm.m4804.entity.DelivBillAggVO;
import nc.vo.dm.m4804.entity.DelivBillBVO;
import nc.vo.dm.m4804.entity.DelivBillHVO;
import nc.vo.dm.m4804.entity.DelivBillPackVO;
import nc.vo.dm.m4804trantype.entity.M4804TranTypeVO;
import nc.vo.dm.m4804trantype.enumeration.FAllocateScopeFlag;
import nc.vo.ml.NCLangRes4VoTransl;
import nc.vo.pub.lang.UFDate;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nc.vo.pubapp.pattern.model.entity.bill.IBill;
import nc.vo.pubapp.pattern.pub.PubAppTool;
import nc.vo.scmpub.util.VOFieldLengthChecker;




















public class BillDataCheck implements IRule<DelivBillAggVO> {
  private Map<String, M4804TranTypeVO> tranTypeVos;
  
  public BillDataCheck(Map<String, M4804TranTypeVO> tranTypeVo) {
    this.tranTypeVos = tranTypeVo;
  }


  public void process(DelivBillAggVO[] vos) {
    for (DelivBillAggVO bill : vos)
      processBillDataCheck(bill); 


    VOFieldLengthChecker.checkVOFieldsLength((IBill[])vos);
  }

  private void checkBillBody(DelivBillHVO header, DelivBillBVO[] items) {
    try {
      String tranTypeCode = header.getVtrantypecode();
      M4804TranTypeVO typeVO = this.tranTypeVos.get(tranTypeCode);
      if (items == null || items.length == 0) {
        if (FAllocateScopeFlag.INV.equalsValue(typeVO.getFallocatescopeflag())) {

          String message = NCLangRes4VoTransl.getNCLangRes().getStrByID("4014001_0", "04014001-0328");


          ExceptionUtils.wrappBusinessException(message);
        } 
        return;
      } 
      int index = 1;
      for (DelivBillBVO item : items)
        checkBillBodyRow(item, index++); 



    } catch (Exception e) {
      ExceptionUtils.wrappException(e);
    } 
  }

  private void checkBillBodyRow(DelivBillBVO item, int i) {
    if (item.getNnum() == null) {

      String message = NCLangResOnserver.getInstance().getStrByID("4014001_0", "04014001-0024", null, new String[] { Integer.toString(i) });



      ExceptionUtils.wrappBusinessException(message);
















    } 
    String ctakefeeid = item.getCtakefeeid();
    String ccosignid = item.getCcosignid();
    checkTakeFeerAndCosigner(ctakefeeid, ccosignid);
  }

  private void checkPackBody(DelivBillHVO header, DelivBillPackVO[] items) {
    try {
      String tranTypeCode = header.getVtrantypecode();
      M4804TranTypeVO typeVO = this.tranTypeVos.get(tranTypeCode);
      if (items == null || items.length == 0) {
        if (FAllocateScopeFlag.PACK.equalsValue(typeVO.getFallocatescopeflag())) {

          String message = NCLangRes4VoTransl.getNCLangRes().getStrByID("4014001_0", "04014001-0329");


          ExceptionUtils.wrappBusinessException(message);
        } 
        return;
      } 
      for (DelivBillPackVO item : items) {
        String ctakefeeid = item.getCtakefeeid();
        String ccosignid = item.getCcosignid();
        checkTakeFeerAndCosigner(ctakefeeid, ccosignid);

      } 
    } catch (Exception e) {
      ExceptionUtils.wrappException(e);
    } 
  }







  private void checkTakeFeerAndCosigner(String ctakefeeid, String ccosignid) {
    if (ccosignid != null && ctakefeeid == null) {

      String message = NCLangRes4VoTransl.getNCLangRes().getStrByID("4014001_0", "04014001-0330");

      ExceptionUtils.wrappBusinessException(message);

    } else if (ctakefeeid != null && ccosignid == null) {

      String message = NCLangRes4VoTransl.getNCLangRes().getStrByID("4014001_0", "04014001-0331");

      ExceptionUtils.wrappBusinessException(message);
    } 
  }

  private void checkTranType(DelivBillAggVO bill) {
    String tranTypeCode = bill.getParentVO().getVtrantypecode();
    M4804TranTypeVO tranType = this.tranTypeVos.get(tranTypeCode);
    Integer allocatescopeflag = tranType.getFallocatescopeflag();

    if (FAllocateScopeFlag.INV.equalsValue(allocatescopeflag)) {
      if (bill.getDelivBillBVO() == null || (bill.getDelivBillBVO()).length == 0) {

        String message = NCLangRes4VoTransl.getNCLangRes().getStrByID("4014001_0", "04014001-0332");

        ExceptionUtils.wrappBusinessException(message);

      } 
    } else if (FAllocateScopeFlag.PACK.equalsValue(allocatescopeflag) && (
      bill.getDelivBillPackVO() == null || (bill
      .getDelivBillPackVO()).length == 0)) {

      String message = NCLangRes4VoTransl.getNCLangRes().getStrByID("4014001_0", "04014001-0333");

      ExceptionUtils.wrappBusinessException(message);
    } 
  }


  private void processBillDataCheck(DelivBillAggVO bill) {
    DelivBillHVO header = bill.getParentVO();
    UFDate dbilldate = header.getDbilldate();
    UFDate ddelivdate = header.getDdelivdate();
    if (dbilldate == null) {

      String message = NCLangRes4VoTransl.getNCLangRes().getStrByID("4014001_0", "04014001-0334");

      ExceptionUtils.wrappBusinessException(message);

    } else if (ddelivdate == null) {

      String message = NCLangRes4VoTransl.getNCLangRes().getStrByID("4014001_0", "04014001-0335");

      ExceptionUtils.wrappBusinessException(message);

    } else if (ddelivdate.beforeDate(dbilldate)) {

      String message = NCLangRes4VoTransl.getNCLangRes().getStrByID("4014001_0", "04014001-0336");

      ExceptionUtils.wrappBusinessException(message);






    } else if (PubAppTool.isNull(header.getCtrantypeid())) {

      String message = NCLangRes4VoTransl.getNCLangRes().getStrByID("4014001_0", "04014001-0337");

      ExceptionUtils.wrappBusinessException(message);







    } else if (header.getCsendtypeid() == null) {

      String message = NCLangRes4VoTransl.getNCLangRes().getStrByID("4014001_0", "04014001-0339");

      ExceptionUtils.wrappBusinessException(message);
    } 
    checkTranType(bill);

    DelivBillBVO[] billItems = bill.getDelivBillBVO();
    checkBillBody(header, billItems);
    DelivBillPackVO[] packItems = bill.getDelivBillPackVO();
    checkPackBody(header, packItems);
  }
}
